<?php if (!defined("_ECRIRE_INC_VERSION")) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

include_spip('functions/pdk_common');
include_spip('classes/FeedbackEmail');
include_spip('classes/phpmailer/class.phpmailer');
include_spip('formulaires/alohacart_cart_order/config');


function formulaires_alohacart_cart_order_traiter_dist(){    
	if (!_request('url')) {        
        $cart = pdk_load_class('Alohacart', 'classes');
        $client_mdl = pdk_load_class('AlohaCartClientModel', 'classes');
        $order_mdl = pdk_load_class('AlohaCartOrderModel', 'classes');
        $currency_mdl = pdk_load_class('CurrencyModel', 'classes');
        $extension_mdl = pdk_load_class('AlohacartExtensionModel','classes/');
        
        $config = $GLOBALS['config_formulaire_alohacart_cart_order'];
        // If adimin emails are configured via CFG, take them
        if (lire_config('alohacart/orders_emails'))
            $config['admin_email'] = lire_config('alohacart/orders_emails');
        $feedback_email = new FeedbackEmail($config['form_name'], $config);
        
        // Create order in DB:
        $id_order = $order_mdl->CreateOrder($_REQUEST);
        set_request('id_order', $id_order);
            
        // REGISTER CLIENT
        $client_mdl->RegisterClient($_REQUEST);
        
        // Add some needed data about products
        $_REQUEST['products_ids'] = $cart->GetProductsIDs(); // Just products IDs
        $_REQUEST['products'] = $cart->GetProducts(); // Products quantities, actually  
        //$_REQUEST['products_prices'] = $cart->GetProductsTotals(); // Products total prices
        $_REQUEST['products_prices'] = $cart->CountPrice(); // Products total prices
        $_REQUEST['id_order'] = $id_order;
        $order = $order_mdl->select_by_id($id_order);
        $_REQUEST['shipping_cost'] = $currency_mdl->ConvertPriceToCurrentCurrency($order['shipping_cost']);
        if (!$_REQUEST['shipping_cost']) {
            unset($_REQUEST['shipping_cost']);
        } else {
            $_REQUEST['order_total_price'] = $currency_mdl->ConvertPriceToCurrentCurrency($order['total_price']);    
        } 
        $_REQUEST['shipping_titre'] = extraire_multi($order['shipping_titre']);
        $_REQUEST['payment_titre'] = extraire_multi($order['payment_titre']);
        if (_request('id_payment')) {
            $extension = $extension_mdl->GetExtensionClassByID(_request('id_payment'));
            $_REQUEST['payment_data'] = $extension->GetPayLink($id_order);
            if (!$_REQUEST['payment_data'])
                $_REQUEST['payment_data'] = $extension->GetData();
        }
        
        // Admin will receive message in site's main language:
        $spip_lang = $GLOBALS['spip_lang'];
        $GLOBALS['spip_lang'] = $GLOBALS['meta']['langue_site'];
        // SEND ADMIN MESSAGE:
        $feedback_email->sendAdminMessage();        
        // Return curent language to the previous state:
        $GLOBALS['spip_lang'] = $spip_lang;
    
        // SEND USER EMAIL IF NEEDED:
        if (lire_config('alohacart/send_usr_email') and _request('email')) {
            $feedback_email->sendUserMessage(_request('email'));
        }
    
        $cart->ClearCart();
		
		// Delete uploaded files
		$files_data = $GLOBALS['uploaded_files_data_alohacart_cart_order']; 	
		if (isset($files_data) && is_array($files_data) && !empty($files_data)) {
			foreach ($files_data as $file_data) {
				if ($file_data['delete_after_use'])
					unlink($file_data['full_path']);					
			}
		}
    }
    
    // If payment selected - add payment data to success message
    if (_request('id_payment') && $_REQUEST['payment_data']) {
        $payment_data = '<div class="payment_data">'.$_REQUEST['payment_data'].'</div>';     
    }
    
    $message_ok = '<div class="message_ok">'._T('alohacart:your_order_accepted').'</div>';
    
    return array('message_ok'=>$message_ok.$payment_data);
}